package com.allm.ai.common.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.concurrent.ConcurrentMapCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 缓存配置类
 * 
 * @author com.enplatform.assistant
 * @since 1.0.0
 */
@Slf4j
@Configuration
@EnableCaching
@ConditionalOnProperty(name = "allm.ai.common.cache.enable", havingValue = "true", matchIfMissing = true)
public class CacheConfiguration {

    /**
     * 配置缓存管理器
     * 使用内存缓存，适合单机部署
     */
    @Bean
    public CacheManager cacheManager() {
        ConcurrentMapCacheManager cacheManager = new ConcurrentMapCacheManager();
        
        // 预定义缓存名称
        cacheManager.setCacheNames(java.util.Arrays.asList(
            "embeddings",           // 嵌入向量缓存
            "excel-metadata",       // Excel 元数据缓存
            "search-results",       // 搜索结果缓存
            "knowledge-base"        // 知识库缓存
        ));
        
        // 允许动态创建缓存
        cacheManager.setAllowNullValues(false);
        
        log.info("Configured ConcurrentMapCacheManager with predefined caches: {}", 
            cacheManager.getCacheNames());
        
        return cacheManager;
    }
}
